package com.cyx.gametool.cons;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * excel中的数据类型枚举
 */
@Getter
public enum ExcelDataType {

    String("VARCHAR", String.class, String.class),

//    JSONArray("VARCHAR", cn.hutool.json.JSONArray.class, String.class),

    INT("INT", Integer.class, Number.class),

    BIGINT("BIGINT", Long.class, Number.class),

    FLOAT("FLOAT", Float.class, Number.class),

    DOUBLE("DOUBLE", Double.class, Number.class),

    BOOLEAN("BOOLEAN", Boolean.class, Boolean.class),

    DATETIME("DATETIME", java.util.Date.class, String.class),

    TIMESTAMP("TIMESTAMP", Integer.class, Number.class),

    ;

    private final static Map<String, ExcelDataType> typeMap = new HashMap<>();

    static {
        for (ExcelDataType excelDataType : ExcelDataType.values()) {
            typeMap.put(excelDataType.name().toLowerCase(), excelDataType);
        }
    }

    private final String mysqlType;
    private final Class<?> javaType;
    private final Class<?> jsonType;

    ExcelDataType(String mysqlType, Class<?> javaType, Class<?> jsonType) {
        this.mysqlType = mysqlType;
        this.javaType = javaType;
        this.jsonType = jsonType;
    }

    public static ExcelDataType getByType(String type) {
        return typeMap.get(type.toLowerCase());
    }

    public boolean checkType(boolean isArray, Object object) {
        if (isArray) {
            return JSONUtil.isTypeJSONArray(object.toString());
        } else {
            return Convert.convert(javaType, object) != null;
        }
    }
}
